<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>序列化二叉树 | 苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="介绍请实现两个函数，分别用来序列化和反序列化二叉树 二叉树的序列化是指：把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串，从而使得内存中建立起来的二叉树可以持久保存。序列化可以基于先序、中序、后序、层序的二叉树遍历方式来进行修改，序列化的结果是一个字符串，序列化时通过 某种符号表示空节点（#），以 ！ 表示一个结点值的结束（value!）。 二叉树的反序列化是指：根据某种遍历顺序得到的序列">
<meta property="og:type" content="article">
<meta property="og:title" content="序列化二叉树">
<meta property="og:url" content="http://dontworryneedmoney.gitee.io/guolianbo/2020/04/29/dontworry/Algorithm/%E5%BA%8F%E5%88%97%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91/index.html">
<meta property="og:site_name" content="苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒">
<meta property="og:description" content="介绍请实现两个函数，分别用来序列化和反序列化二叉树 二叉树的序列化是指：把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串，从而使得内存中建立起来的二叉树可以持久保存。序列化可以基于先序、中序、后序、层序的二叉树遍历方式来进行修改，序列化的结果是一个字符串，序列化时通过 某种符号表示空节点（#），以 ！ 表示一个结点值的结束（value!）。 二叉树的反序列化是指：根据某种遍历顺序得到的序列">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-04-29T07:12:43.397Z">
<meta property="article:modified_time" content="2020-04-29T09:04:36.132Z">
<meta property="article:author" content="worry dont">
<meta property="article:tag" content="Algorithm">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/guolianbo/atom.xml" title="苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/guolianbo/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/guolianbo/" id="logo">苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/guolianbo/">Home</a>
        
          <a class="main-nav-link" href="/guolianbo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/guolianbo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://dontworryneedmoney.gitee.io/guolianbo"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="ejs-dontworry/Algorithm/序列化二叉树" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2020/04/29/dontworry/Algorithm/%E5%BA%8F%E5%88%97%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91/" class="article-date">
  <time datetime="2020-04-29T07:12:43.397Z" itemprop="datePublished">2020-04-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      序列化二叉树
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p>请实现两个函数，分别用来序列化和反序列化二叉树</p>
<p>二叉树的序列化是指：把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串，从而使得内存中建立起来的二叉树可以持久保存。序列化可以基于先序、中序、后序、层序的二叉树遍历方式来进行修改，序列化的结果是一个字符串，序列化时通过 某种符号表示空节点（#），以 ！ 表示一个结点值的结束（value!）。</p>
<p>二叉树的反序列化是指：根据某种遍历顺序得到的序列化字符串结果str，重构二叉树。</p>
<p>例如，我们可以把一个只有根节点为1的二叉树序列化为”1,”，然后通过自己的函数来解析回这个二叉树</p>
<h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> tree0 = <span class="keyword">new</span> TreeNode(<span class="number">8</span>);</span><br><span class="line"><span class="keyword">const</span> tree1 = <span class="keyword">new</span> TreeNode(<span class="number">6</span>);</span><br><span class="line"><span class="keyword">const</span> tree2 = <span class="keyword">new</span> TreeNode(<span class="number">10</span>);</span><br><span class="line"><span class="keyword">const</span> tree3 = <span class="keyword">new</span> TreeNode(<span class="number">5</span>);</span><br><span class="line"><span class="keyword">const</span> tree4 = <span class="keyword">new</span> TreeNode(<span class="number">7</span>);</span><br><span class="line"><span class="keyword">const</span> tree5 = <span class="keyword">new</span> TreeNode(<span class="number">9</span>);</span><br><span class="line"><span class="keyword">const</span> tree6 = <span class="keyword">new</span> TreeNode(<span class="number">11</span>);</span><br><span class="line">tree0.left = tree1;</span><br><span class="line">tree0.right = tree2;</span><br><span class="line">tree1.left = tree3;</span><br><span class="line">tree1.right = tree4;</span><br><span class="line">tree2.left = tree5;</span><br><span class="line">tree2.right = tree6;</span><br><span class="line"><span class="built_in">console</span>.log(Serialize(tree0));</span><br><span class="line"><span class="built_in">console</span>.log(Deserialize(Serialize(tree0)));</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">TreeNode</span>(<span class="params">x</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.val = x;</span><br><span class="line">    <span class="keyword">this</span>.left = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">this</span>.right = <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Serialize</span>(<span class="params">pRoot</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// write code here</span></span><br><span class="line">    <span class="keyword">if</span> (!pRoot) <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">let</span> strArray = <span class="string">""</span>;</span><br><span class="line">    <span class="keyword">const</span> queueTree = [];</span><br><span class="line">    <span class="keyword">let</span> currentNode = <span class="literal">null</span>;</span><br><span class="line">    queueTree.push(pRoot);</span><br><span class="line">    <span class="keyword">while</span> (queueTree.length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        currentNode = queueTree.shift();</span><br><span class="line">        <span class="keyword">if</span>( currentNode)&#123;</span><br><span class="line">            strArray = strArray + currentNode.val + <span class="string">";"</span>;</span><br><span class="line">            queueTree.push(currentNode.left);</span><br><span class="line">            queueTree.push(currentNode.right);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            strArray = strArray + <span class="string">'#'</span> + <span class="string">";"</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> strArray;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Deserialize</span>(<span class="params">s</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// write code here</span></span><br><span class="line">    <span class="keyword">if</span> (!s) <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">const</span> strArray = s.split(<span class="string">";"</span>);</span><br><span class="line">    <span class="keyword">let</span> root = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">if</span> (strArray[<span class="number">0</span>]) &#123;</span><br><span class="line">        root = <span class="keyword">new</span> TreeNode(strArray[<span class="number">0</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (!root) <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">const</span> nodeQueue = [];</span><br><span class="line">    nodeQueue.push(root);</span><br><span class="line">    <span class="keyword">let</span> index = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">while</span> (nodeQueue.length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> currentNode = nodeQueue.shift();</span><br><span class="line">        <span class="keyword">if</span>(strArray[index] === <span class="string">'#'</span>)&#123;</span><br><span class="line">            currentNode.left =<span class="literal">null</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            currentNode.left = <span class="keyword">new</span> TreeNode(strArray[index]);</span><br><span class="line">            nodeQueue.push(currentNode.left);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(strArray[index + <span class="number">1</span>] === <span class="string">'#'</span>)&#123;</span><br><span class="line">            currentNode.right =<span class="literal">null</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            currentNode.right = <span class="keyword">new</span> TreeNode(strArray[index + <span class="number">1</span>]);</span><br><span class="line">            nodeQueue.push(currentNode.right);</span><br><span class="line">        &#125;</span><br><span class="line">        index += <span class="number">2</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> root;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>使用广度遍历进行序列化，还是挺简单的，只是会多一些<code>#</code>;</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2020/04/29/dontworry/Algorithm/%E5%BA%8F%E5%88%97%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91/" data-id="ckb6hw6qv0001jcn207816i7c" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/Algorithm/" rel="tag">Algorithm</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/guolianbo/2020/04/29/dontworry/Algorithm/%E6%8C%89%E5%B1%82%E6%89%93%E5%8D%B0%E4%BA%8C%E5%8F%89%E6%A0%91/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          按层打印二叉树
        
      </div>
    </a>
  
  
    <a href="/guolianbo/2020/04/28/dontworry/Algorithm/%E6%90%9C%E7%B4%A2%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E7%AC%ACk%E4%B8%AA%E8%8A%82%E7%82%B9/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">搜索二叉树的第k个节点</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/guolianbo/categories/%E9%9A%8F%E7%AC%94/">随笔</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/Algorithm/" rel="tag">Algorithm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/Unity3d/" rel="tag">Unity3d</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" rel="tag">杂项</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E6%B6%88%E9%98%B2/" rel="tag">消防</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E7%94%B3%E8%AE%BA/" rel="tag">申论</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E9%9A%8F%E7%AC%94/" rel="tag">随笔</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/guolianbo/tags/Algorithm/" style="font-size: 14px;">Algorithm</a> <a href="/guolianbo/tags/Unity3d/" style="font-size: 12px;">Unity3d</a> <a href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" style="font-size: 18px;">杂项</a> <a href="/guolianbo/tags/%E6%B6%88%E9%98%B2/" style="font-size: 16px;">消防</a> <a href="/guolianbo/tags/%E7%94%B3%E8%AE%BA/" style="font-size: 10px;">申论</a> <a href="/guolianbo/tags/%E9%9A%8F%E7%AC%94/" style="font-size: 20px;">随笔</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/06/">June 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/05/">May 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/04/">April 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/03/">March 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/02/">February 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/01/">January 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/11/">November 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/09/">September 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/07/">July 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/06/">June 2019</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/guolianbo/2020/06/08/dontworry/Unity3d/IObserver%E5%92%8CIObservable%E5%92%8CIDisposable/">IObserver和IObservable和IDisposable</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/28/dontworry/%E6%9D%82%E9%A1%B9/javascript%E7%9A%84%E6%9C%AC%E8%B4%A8/">javascript的本质</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/27/dontworry/%E6%9D%82%E9%A1%B9/%E5%8D%8F%E7%A8%8B%E7%9A%84%E5%8E%9F%E7%90%86/">协程的原理</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/20/dontworry/%E6%9D%82%E9%A1%B9/linux%E4%B8%8B%E7%9A%84nginx%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/">linux下的nginx的基本操作</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/15/dontworry/%E6%9D%82%E9%A1%B9/%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%E5%8A%A8%E8%AF%8D/">常用函数动词</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 worry dont<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/guolianbo/" class="mobile-nav-link">Home</a>
  
    <a href="/guolianbo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/guolianbo/fancybox/jquery.fancybox.css">

  
<script src="/guolianbo/fancybox/jquery.fancybox.pack.js"></script>




<script src="/guolianbo/js/script.js"></script>




  </div>
</body>
</html>